package com.foreknow.demo14;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PrepareStatementDemo {
    public static void main(String[] args) {
        try {
            //1.加载驱动 jdbc驱动类：5.7:com.mysql.jdbc.Driver  8.0:com.mysql.cj.jdbc.Driver
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/basic5?useUnicode=true&characterEncoding=utf8",
                    "root", "root");
            //3.获取prepareStatement对象用于执行动态SQL
            PreparedStatement pstmt =
                    conn.prepareStatement("insert into guestbook(name,phone,email,title,content)" +
                    " values(?,?,?,?,?)");
            //将？替换具体的值
            pstmt.setString(1,"fender");
            pstmt.setString(2,"13811111111");
            pstmt.setString(3,"fender@163.com");
            pstmt.setString(4,"title");
            pstmt.setString(5,"test!!!");
            int i = pstmt.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
